package edu.chl.jadetalk.view;

import edu.chl.jadetalk.business.ImageManagerBeanLocal;
import edu.chl.jadetalk.core.Comment;
import javax.ejb.EJB;
import edu.chl.jadetalk.core.Image;
import edu.chl.jadetalk.core.Rating;
import edu.chl.jadetalk.core.User;
import edu.chl.jadetalk.user.UserSessionBean;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author jesper
 */
@ManagedBean(name = "imageViewBackingBean")
@RequestScoped
public class ImageViewBackingBean {

    @ManagedProperty(value = "#{userSessionBean}")
    private UserSessionBean userSessionBean;
        
    @EJB
    private ImageManagerBeanLocal imageManagerBean;
    
    private long entryId;

    public ImageViewBackingBean() {
        Logger.getAnonymousLogger().log(Level.INFO, "Creating an ImageViewBackingBean");
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

        if (request.getParameter("id") != null) {
            this.entryId = Long.valueOf(request.getParameter("id"));
        }
    }

    public Image getImage() {
        return imageManagerBean.getImage(entryId);
    }

    public long getEntryId() {
        return entryId;
    }

    /**
     * Gets all images from database
     * @return List of images
     */
    public List<Image> getAllImages() {
        return imageManagerBean.listImages();
    }
    
    public List<Image> getLatest() {
        return imageManagerBean.listImagesRange(new int[] {0,8});
    }

    
    public double getAverageRating(){
        return getImage().getRating();
    }
    
    public double getMyRating(){
        Rating rating = getUserRating();
        return rating != null ? (double)rating.getValue() : 0;
    }
    
    public boolean getCanRate(){
        return getUserRating() == null;
    }
    
    private Rating getUserRating(){
        // TODO: Cache rating if found?
        
        // Find user's rating
        User user = userSessionBean.getUser();
        
        for(Rating r : getImage().getRatings()){
            if(r.getUser().equals(user)){
                return r;
            }
        }
        
        return null;
    }
    
    public Collection<Comment> getComments() {
        
        // Returns comments sorted on time
        List<Comment> comments = imageManagerBean.getImage(entryId).getCommentThread().getComments();
        
        Collections.sort(comments, new Comparator<Comment>(){
            @Override
            public int compare(Comment a, Comment b) {
                Date aAdded = a.getAdded();
                Date bAdded = b.getAdded();
                
                if(aAdded == null || bAdded == null){
                    return 0;
                }
                
                return aAdded.compareTo(bAdded);
            }
        });
        
        return comments;
    }
    
    public UserSessionBean getUserSessionBean() {
        return userSessionBean;
    }

    public void setUserSessionBean(UserSessionBean userSessionBean) {
        this.userSessionBean = userSessionBean;
    }
}
